/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include <string.h>

#include <jni.h>
#include "scenic_show_SimpleMediaFile.h"

#include "common.h"
#include "SimpleMediaFile.h"

#include "Debug.h"


using namespace Scenic::Renderer;


int getId(JNIEnv *env, jobject obj)
{
	jclass clazz = env->GetObjectClass(obj);
	jfieldID idField = env->GetFieldID(clazz, "id", "I");

	return env->GetIntField(obj, idField);
}

JNIEXPORT jint JNICALL Java_scenic_show_SimpleMediaFile_create
  (JNIEnv *env, jobject obj, jstring filename, jobject imageObj)
{
	Image *image = Image::get(getId(env, imageObj));

	image->addReference(env, imageObj);

	const char *filenameChars = env->GetStringUTFChars(filename, NULL);
	SimpleMediaFile *media = SimpleMediaFile::open(filenameChars, image);

	if(!media)
		return 0;
	media->addReference(env, obj);
	env->ReleaseStringUTFChars(filename, filenameChars);
	media->enumerateOutputFormats();

	return media->id;
}

JNIEXPORT void JNICALL Java_scenic_show_SimpleMediaFile_free
  (JNIEnv *env, jobject, jint id)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(!media)
		return;

	delete media;
}

JNIEXPORT void JNICALL Java_scenic_show_SimpleMediaFile_play
  (JNIEnv *, jobject, jint id)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		media->play();
}

JNIEXPORT void JNICALL Java_scenic_show_SimpleMediaFile_pause
  (JNIEnv *, jobject, jint id)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		media->pause();
}

JNIEXPORT jdouble JNICALL Java_scenic_show_SimpleMediaFile_getCurrentPosition
  (JNIEnv *, jobject, jint id)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		return media->getCurrentPosition();
	return 0.0;
}

JNIEXPORT void JNICALL Java_scenic_show_SimpleMediaFile_setCurrentPosition
  (JNIEnv *, jobject, jint id, jdouble position)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		media->setCurrentPosition(position);
}

JNIEXPORT jdouble JNICALL Java_scenic_show_SimpleMediaFile_getLength
  (JNIEnv *, jobject, jint id)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		media->getLength();
	return 0.0;
}

JNIEXPORT void JNICALL Java_scenic_show_SimpleMediaFile_setOutputFormat
  (JNIEnv *, jobject, jint id, jint width, jint height, jint format)
{
	SimpleMediaFile *media = SimpleMediaFile::get(id);

	if(media)
		media->setOutputFormat(width, height, (Format)format);
}
